package com.todo.event.activity.application.handler.activityrule;

import com.alibaba.fastjson.JSONObject;
import com.todo.event.activity.application.entity.bo.ActivityRecordBO;
import com.todo.event.activity.application.entity.context.ActivityRuleContext;
import com.todo.event.activity.application.service.EventActivityRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author azhebuxing
 * @date 2025/1/13 15:12
 * @description
 */
@Slf4j
@Component
public class OperateRecordHandle extends AbstractActivityRuleHandle {

    @Resource
    private EventActivityRecordService eventActivityRecordService;

    @Override
    protected String ruleName() {
        return "OperateRecord";
    }

    @Override
    protected boolean doRuleHandle(ActivityRuleContext context) {
        //存在数据我们认为当前活动执行成功，即使子逻辑出现问题，也应该由子逻辑的保障策略控制
        ActivityRecordBO activityRecordBO = eventActivityRecordService.queryActivityRecord(context.getActivityId(), context.getUserId(), context.getUk());
        if (activityRecordBO != null){
            log.info("OperateRecordHandle#OperateRecordHandle#ruleMatch 规则过滤发现存在历史数据，默认执行通过 ActivityRuleContext:【{}】 activityRecordBO:【{}】",JSONObject.toJSONString(context), JSONObject.toJSONString(activityRecordBO));
            return false;
        }
        return true;
    }

    @Override
    protected boolean ruleMatch(ActivityRuleContext context) {
        return true;
    }
}
